﻿// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2023 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

using Furion.DynamicApiController;
using Furion.SpecificationDocument;
using Microsoft.AspNetCore.Authorization;

namespace ERAS.Application.API.Swagger;

[ApiDescriptionSettings("SaggerAuth", Name = "SaggerAuth", IgnoreApi = true)]
//[ApiDescriptionSettings(IgnoreApi = false)]
//[ApiExplorerSettings(IgnoreApi = true)]
[MaApi(  controller: "SaggerAuth", controllerDescription: "Swagger授权", PCode = "API")]
[IgnoreAntiforgeryToken]
public class SaggerAuthService : IDynamicApiController
{
    private readonly ISqlSugarClient _db;
    public SaggerAuthService(ISqlSugarClient db)
    {
        _db = db;
    }
    /// <summary>
    /// 检查登录状态
    /// </summary>
    /// <returns></returns>
    [HttpPost, AllowAnonymous, NonUnify]
    public int CheckUrl()
    {
        return 401;
    }

    /// <summary>
    /// Swagger 登录授权
    /// </summary>
    /// <param name="auth"></param>
    /// <returns></returns>
    [HttpPost, AllowAnonymous, NonUnify]
    public int SubmitUrl([FromForm] SpecificationAuth auth)
    {
        var user = _db.Queryable<SysUser>().First(o => o.Account.ToLower() == auth.UserName.ToLower() && o.IsSwagger == true);
        if (user == null)
        {
            return 401;
        }
        if (!auth.Password.ToPBKDF2Compare2(user.Pwd))
        {
            return 401;
        }

        return 200;
    }
}
